<template>
  <el-dialog :title="title + '初审'" :visible.sync="isShow" @close="$emit('update:isShowAudit', false)" width="500px" :close-on-click-modal="false">
    <el-form label-position="right" ref="info" :rules="rules" :model="info" label-width="110px">
      <el-form-item :label="title + '发放:'">
        <el-radio v-model="info.status" :label="1">通过</el-radio>
        <el-radio v-model="info.status" :label="2">驳回</el-radio>
      </el-form-item>
      <div  v-if="info.status === 1">
        <el-row v-if="info.type === 2">
          <el-col :span="13">
            <el-form-item label="初审额度:" :rules="rule" prop="grantCredit">
              <el-input v-model="info.grantCredit">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="账期:" prop="count">
              <el-select v-model="info.count" placeholder="全部">
                <el-option v-for="n in countLimit" :key="n" :label="n + '个月'" :value="n"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item v-else label="初审额度:" :rules="rule" prop="grantCredit">
          <el-input v-model="info.grantCredit">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </div>
      <el-form-item label="原因:" prop="reason">
        <el-input type="textarea" v-model="info.reason"
                  :autosize="{ minRows: 3, maxRows: 8}" placeholder="请输入内容"></el-input>
      </el-form-item>
    </el-form>
    <div class="footer text-center mt20">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="showSecondaryAlert">确 定</el-button>
    </div>
    <SecondaryConfirm
      @confirmAmout="confirmAmout"
      :isShowDetail.sync="isShowSecondary"
      :dataInfo="info"
      :type.sync="secondaryType"></SecondaryConfirm>
  </el-dialog>
</template>

<script>
  import SecondaryConfirm from './SecondaryConfirm'
  export default {
    props: {
      isShowAudit: {
        type: Boolean,
        default: false
      },
      auditInfo: {
        type: Object,
        default: () => {
          return {
            type: 2,
            applyId: "",
            grantCredit: ""
          }
        }
      }
    },
    components: {
      SecondaryConfirm
    },
    data() {
      return {
        title: "固定额度",
        isShow: this.isShowGrant,
        isShowSecondary: false,
        secondaryType: 2,
        info: {...this.auditInfo, status: 1, reason: "",count:0},
        countLimit:0,
        rules: {
          reason: [
            {required: true, message: '请输入原因', trigger: 'blur'},
            {validator: validate.length150, trigger: 'change'}
          ]
        },
        rule: [{required: true, message: '请输入初审额度', trigger: 'blur'},
          {validator: validate.price2, trigger: 'change'}],
      }
    },
    watch: {
      isShowAudit(val) {
        this.isShow = val
      },
      auditInfo(val) {
        this.title = val.type === 2 ? "固定额度" : val.type === 1 ? "临时额度" : ""
        this.info = {...val, status: 1, reason: "",count:0}
        if(val.type === 2){
          this.getSchemaLimit(val.applyId)
        }
      }
    },
    methods: {
      // 二次确认弹框
      confirmAmout() {
        this.onSubmit()
      },
      onSubmit() {
        let info = {
          applyId: this.info.applyId,
          reason: this.info.reason,
          grantCredit: this.info.grantCredit,
        }

        if(this.info.type === 2) {
          info = {...info,count:this.info.count}
          this.fixedPass(info)
        } else {
          this.temporaryPass(info)
        }

      },
      showSecondaryAlert() {
        this.$refs['info'].validate((valid) => {
          if (valid) {
            if (this.info.status === 2) {
              let info = {
                applyId: this.info.applyId,
                reason: this.info.reason
              }
              this.applyReject(info)
            }else{
              this.$refs['info'].validateField("grantCredit",(res) => {
                if (res === "") {
                  let info = {
                    applyId: this.info.applyId,
                    grantCredit: this.info.grantCredit,
                    reason: this.info.reason
                  }
                  if (this.info.type === 2){
                    if(this.info.count < 1){
                      this.$message({
                        message: "请选择账期",
                        type: 'error'
                      });
                    }else{
                      this.secondaryType = 2
                      this.isShowSecondary = true;
                    }
                  }else{
                    this.secondaryType = 1
                    this.isShowSecondary = true;
                  }
                } else {
                  return false;
                }
              })
            }
          }
          else {
            return false;
          }
        });

      },
      // 驳回
      applyReject(info) {
        ajax.post(apis.factory.applyReject, info)
          .then(res => {
            this.resultHandle(res)
          })
      },
      // 固定额度通过
      fixedPass(info) {
        ajax.post(apis.factory.fixedPass, info)
          .then(res => {
            this.resultHandle(res)
          })
      },
      // 账期最大值获取
      getSchemaLimit(id) {
        ajax.get(apis.factory.getCreditApplySchemaLimit, {id:id})
          .then(res => {
            if (res.success === true) {
              this.countLimit = res.data
              if(this.countLimit > 0){
                this.$set(this.info,"count",1)
              }
            }else{
              this.countLimit = 0
            }
          })
      },
      // 临时额度通过
      temporaryPass(info) {
        ajax.post(apis.factory.temporaryPass, info)
          .then(res => {
            this.resultHandle(res)
          })
      },
      // 结果处理
      resultHandle(res){
        if (res.success === true) {
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.isShow = false
          this.isShowSecondary = false
          this.$emit("refreshCurrentPage")
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      }
    },
    created() {
    }
  }
</script>

<style lang="scss" scoped>
</style>
